package com.qzdsoft.erpcloud.controller.proj;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qzdsoft.common.domain.query.sys.QueryParams;
import com.qzdsoft.erpcloud.controller.BaseController;
import com.qzdsoft.erpcloud.domain.proj.MaterialInfo;
import com.qzdsoft.erpcloud.domain.sys.LoginUser;
import com.qzdsoft.erpcloud.service.basic.DataDictionaryValueService;
import com.qzdsoft.erpcloud.service.basic.MeasurementUnitService;
import com.qzdsoft.erpcloud.service.proj.MaterialInfoService;
import com.qzdsoft.erpcloud.service.sys.Permission;
import com.qzdsoft.erpcloud.service.sys.SysUserService;
import com.qzdsoft.erpcloud.vo.proj.MaterialInfoQueryInfo;
import com.qzdsoft.erpcloud.vo.proj.ProjMaterialInfo;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.Response;
import com.qzdsoft.vo.TypeInfo;
import com.qzdsoft.vo.pu.PurchaseOrderStatus;

/**
 * Auth:ZhangHao Date:2017-08-08
 */
@Controller
@RequestMapping("/eng/material/info")
public class MaterialInfoController extends BaseController
{
    private static final String BASEPATH = "eng/materialinfo";

    private static final Logger logger = LoggerFactory
            .getLogger(MaterialInfoController.class);

    @Autowired
    private MaterialInfoService materialService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private MeasurementUnitService unitService;

    @Autowired
    private DataDictionaryValueService enumValueService;

    /**
     * 物料信息 初始化页面
     * @param request
     * @param model
     * @param params
     * @return
     */
    @Permission(validate=false)
    @RequestMapping(value = "index", method = RequestMethod.GET)
    public ModelAndView index(HttpServletRequest request,QueryParams params){
        ModelAndView mv = new  ModelAndView();
     // 获取物料分类信息 列表 根据分组id 拿数据
        mv.addObject("materialTypeList", enumValueService.getByEnumId(7));
        // 获取物料品牌信息 列表
        mv.addObject("materialBrandList", enumValueService.getByEnumId(2));
        mv.setViewName("eng/materialinfo/index");
        return mv;
    }

    /**
     * 初始化网格数据
     * @param request
     * @param model
     * @param params
     * @return
     */
    @RequestMapping("/page")
    @ResponseBody
    public LayTableResponse<ProjMaterialInfo> list(int page, int limit)throws Exception{
        // layui table 网格数据源
        LayTableResponse<ProjMaterialInfo> tabledata = materialService
                .findPage(page, limit);
        return tabledata;
    }

    /**
     * 打开物料信息新增页面
     * 
     * @param model
     * @param request
     * @return
     */
    @Permission(validate=false)
    @RequestMapping("/goforadd")
    public ModelAndView detail(int companyid, Integer id)
    {
        ModelAndView mv = new ModelAndView();
        // 获取物料分类信息 列表 根据分组id 拿数据
        mv.addObject("materialTypeList", enumValueService.getByEnumId(7));
        // 获取物料品牌信息 列表
        mv.addObject("materialBrandList", enumValueService.getByEnumId(2));
        // 获取计价方式信息 列表
        mv.addObject("priceTypeList", enumValueService.getByEnumId(8));
        // 获取UnitList 计量单位
        mv.addObject("unitList", unitService.getAllUnit());
        List<TypeInfo> status = PurchaseOrderStatus.ALL;
        mv.addObject("status", status);
        mv.setViewName(BASEPATH + "/addinfo");
        return mv;
    }

    /**
     * 新增物料信息保存
     * @param materialInfo
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/addMaterial")
    public Response<String> addMenu(MaterialInfo materialInfo,HttpServletRequest request) throws Exception{
        LoginUser loginUser = getUser();
        // 提交保存
        return materialService.addMaterial(materialInfo, loginUser);
    }
    
    /**
     * 打开请购单表头修改页面
     * 
     * @param model
     * @param request
     * @return
     * @throws Exception 
     */
    @Permission(validate=false)
    @RequestMapping("/goforedit")
    public ModelAndView showeditinfo(int companyid,Integer id) throws Exception {
        ModelAndView mv = new ModelAndView();
        // 获取选中记录详情 视图
        mv.addObject("entity", materialService.getViewById(id));
        // 获取物料分类信息 列表 根据分组id 拿数据
        mv.addObject("materialTypeList", enumValueService.getByEnumId(7));
        // 获取物料品牌信息 列表
        mv.addObject("materialBrandList",
                enumValueService.getByEnumId(2));
        // 获取计价方式信息 列表
        mv.addObject("priceTypeList", enumValueService.getByEnumId(8));
        // 测试 写回物料编码code
        mv.addObject("test", "00001");
        // 获取UnitList
        mv.addObject("unitList", unitService.getAllUnit());
        List<TypeInfo> status = PurchaseOrderStatus.ALL;
        mv.addObject("status",status);
        mv.setViewName(BASEPATH+"/editinfo");
        return mv;
    }
    
    /**
     * 修改物料信息
     * 
     * @param materialInfo
     * @param request
     * @return
     */
    @RequestMapping("/editMaterial")
    @ResponseBody
    public Response<String> editMenu(@ModelAttribute MaterialInfo materialInfo,HttpServletRequest request){
        LoginUser loginUser = getUser();
        return materialService.editMaterial(materialInfo, loginUser);
    }
    
    /**
     * 采购入库单据查看
     * @param id
     * @return
     */
     @Permission(validate=false)
     @RequestMapping("/view/{id}")
     public ModelAndView view(@PathVariable("id")Integer id) {
         logger.debug("物料信息查看:id={}",id);
         ModelAndView mv = new ModelAndView();
         mv.addObject("entity", materialService.getViewById(id));
         mv.setViewName(BASEPATH+"/view");
         return mv;
     }
    /**
     * 删除物料信息
     * 
     * @param id
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/deleteMaterial")
    public Response<String> deleteMenu(Integer id, HttpServletRequest request)
    {
        // 检查session
        LoginUser loginUser = getUser();
        return materialService.delteMaterial(id, loginUser);
    }

    /**
     * 搜索查询
     * @param request
     * @param response
     * @param page
     * @param rows
     * @return
     * @throws IOException
     */
    @Permission(validate=false)
    @RequestMapping("/data")
    @ResponseBody
    public LayTableResponse<ProjMaterialInfo> searchlist(MaterialInfoQueryInfo queries)throws Exception{
        // layui table 网格数据源
        LayTableResponse<ProjMaterialInfo> tabledata = materialService.findPageByItems(queries);
        return tabledata;
    }
}
